//
//  LoginEditController.m
//  Login
//
//  Created by Sean on 4/2/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "LoginEditController.h"
#import "LoginAppDelegate.h"

@implementation LoginEditController



-(id) initWithLogins:(Logins*)allLogins_:(Login*)editLogin_
{
	allLogins = allLogins_;
	editLogin = editLogin_;
	
	return [self initWithNibName:@"LoginEditController" bundle:[NSBundle mainBundle]];
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}


- (EditableDetailCell *)newDetailCellWithTag:(NSInteger)tag:(NSString*)svalue
{
    EditableDetailCell *cell = [[EditableDetailCell alloc] initWithFrame:CGRectZero 
                                                         reuseIdentifier:nil];
    [[cell textField] setDelegate:self];
    [[cell textField] setTag:tag];
	
	if(svalue != nil)
		[[cell textField] setText:svalue];
    
    return cell;
}

- (void)viewDidLoad
{
	aTableView.backgroundColor = [UIColor clearColor];
	//self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
	
	if(editLogin != nil){
		loginName = [self newDetailCellWithTag:1001:[editLogin loginName]];
		key = [self newDetailCellWithTag:1002:[editLogin key]];
		value = [self newDetailCellWithTag:1003:[editLogin value]];
	}
	else
	{
		loginName = [self newDetailCellWithTag:1001:nil];
		key = [self newDetailCellWithTag:1002:nil];
		value = [self newDetailCellWithTag:1003:nil];
	}
	
	//self.navigationItem.hidesBackButton = YES;
	//self.navigationController.navigationBarHidden = YES;
	
}

BOOL isNumericI(NSString *s)
{
	NSUInteger len = [s length];
	NSUInteger i;
	BOOL status = NO;
	
	for(i=0; i < len; i++)
	{
		unichar singlechar = [s characterAtIndex: i];
		if ( (singlechar == ' ') && (!status) )
		{
			continue;
		}
		if ( ( singlechar == '+' ||
              singlechar == '-' ) && (!status) ) { status=YES; continue; }
		if ( ( singlechar >= '0' ) &&
            ( singlechar <= '9' ) )
		{
			status = YES;
		} else {
			return NO;
		}
	}
	return (i == len) && status;
}

- (IBAction) cancelPressed:(id)sender
{
	self.navigationController.navigationBarHidden = NO;
	[self.navigationController popViewControllerAnimated:YES];
}

- (IBAction) donePressed:(id)sender
{
	if(editLogin != nil)
	{
		if([[loginName textField].text length] >0)
		{
			editLogin.loginName = [loginName textField].text;
		}
		
		if([[key textField].text length] >0)
		{
			editLogin.key = [key textField].text;
		}
		
		if([[value textField].text length] >0)
		{
			editLogin.value = [value textField].text;
		}
		
		[LoginAppDelegate setDirty:YES];
		
	}
	else{
		
		if([[loginName textField].text length] >0 && 
		   [[key textField].text length] >0 && 
		   [[value textField].text length] >0)
		{
			
		
		
		
		NSString* letter = [[[loginName textField].text substringToIndex:1] uppercaseString];
		
		
		NSNumberFormatter* numFor = [NSNumberFormatter new];	
			
		if([numFor numberFromString: letter] ==nil)	
			[allLogins addLogin:letter:[loginName textField].text:[key textField].text:[value textField].text];
		else
			[allLogins addLogin:@"#":[loginName textField].text:[key textField].text:[value textField].text];
			
		[numFor release];	
		
			
		[LoginAppDelegate setDirty:YES];	
		}
		
	}
	
	
	
	self.navigationController.navigationBarHidden = NO;
	[self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	if(section == 0)
		return 1;
	else
		return 2;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

	if(section == 0)
		return @"Login Name";
	else
		return @"Values";
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    EditableDetailCell *cell = nil;
    NSString *placeholder = nil;
    
	if(indexPath.section ==0){
		 cell = loginName;
		placeholder =@"";
	}
	else
	{
		if(indexPath.row==0){
			cell = key;
			placeholder =@"login";
			
		}
		else{
			cell = value;
			placeholder =@"password";
		}
		
	}
	
  
    
    UITextField *textField = [cell textField];
    [textField setPlaceholder:placeholder];
    
   
    
    return cell;
}





- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
	[textField resignFirstResponder];
	return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
	
}

- (void)viewDidUnload {
	aTableView = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
